<?php

class Memberprofit extends CS_Controller
{
    public function _init()
    {
        $this->load->model('user_model', 'user');
        $this->load->model('order_profit_model', 'order_profit');
        $this->load->model('tourism_order_product_profit_model', 'tourism_order_product_profit');
        $this->load->model('line_order_profit_model', 'line_order_profit');
    }

    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 2:22
     */
    public function grid($uid = false, $user_name = false)
    {
        $this->data['payer_uid'] = $uid;
        $this->data['user_name'] = $user_name;

        $this->load->view('home/memberprofit/grid', $this->data);
    }


    /**
     * @descripte   异步景区分润
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxScenic()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制  范围在0-6个月*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        if(empty($this->uid)){
            $this->jsonMessages($msg = '用户未登陆', $url = 'ajaxMessages');
            exit;
        }
        $pg = $this->input->get('page') ? (int)$this->input->get('page') : '1';
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->order_profit->profit_page_list($page_num, $num, $getData);
        $data['more'] = ($data['profit']->num_rows() > 0) ? (($data['profit']->num_rows() >= 10) ? true:false ): false;
        /*统计收入*/
        $account = $this->order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['account'] = ($account->num_rows() > 0) ? $account->row()->amount : '0';
        $not_account = $this->order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 1, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['not_account'] = ($not_account->num_rows() > 0) ? $not_account->row()->amount : '0';
        $data['cat_name'] = array(
            1    => '普通票',
            2    => '联票',
            4    => '线路',
            8    => '年票',
            16   => '团队票',
            256  => '预充值',
            512  => '提现',
            1024 => '游币充值',
        );
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxScenic', $data, TRUE),
                )
            );
            exit;
        } else {
            $this->jsonMessages($msg = '还没有景区分润信息', $url = 'ajaxMessages');
            exit;
        }

    }

    /**
     * @descripte   异步景区 分页
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxScenicPage()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制  范围在0-6个月*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        $pg = $this->input->get('pg');
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->order_profit->profit_page_list($page_num, $num, $getData);
        /*统计收入*/
//        $account = $this->order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
//        $data['account'] = ($account->num_rows() > 0) ? $account->row()->amount : '0';
        $data['cat_name'] = array(
            1    => '普通票',
            2    => '联票',
            4    => '线路',
            8    => '年票',
            16   => '团队票',
            256  => '预充值',
            512  => '提现',
            1024 => '游币充值',
        );
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxScenicPage', $data, TRUE),
                )
            );
            exit;
        } else {
            echo json_encode(
                array(
                    'status' => false,
                )
            );
            exit;
        }

    }


    /**
     * @descripte   异步商品分润
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxTourism()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        if(empty($this->uid)){
            $this->jsonMessages($msg = '用户未登陆', $url = 'ajaxMessages');
            exit;
        }
        $pg = $this->input->get('page') ? (int)$this->input->get('page') : '1';
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->tourism_order_product_profit->profit_page_list($page_num, $num, $getData);
        $data['more'] = ($data['profit']->num_rows() > 0) ? (($data['profit']->num_rows() >= 10) ? true:false ): false;
        /*统计收入*/
        $account = $this->tourism_order_product_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['account'] = ($account->num_rows() > 0) ? $account->row()->account : '0';
        $not_account = $this->tourism_order_product_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 1, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['not_account'] = ($not_account->num_rows() > 0) ? $not_account->row()->account : '0';
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxTourism', $data, TRUE),
                )
            );
            exit;
        } else {
            $this->jsonMessages($msg = '还没有商品分润信息', $url = 'ajaxMessages');
            exit;
        }
    }

    /**
     * @descripte   异步商品分润 分页
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxTourismPage()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        $pg = $this->input->get('pg');
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->tourism_order_product_profit->profit_page_list($page_num, $num, $getData);
        /*统计收入*/
//        $account = $this->tourism_order_product_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
//        $data['account'] = ($account->num_rows() > 0) ? $account->row()->account : '0';
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxTourismPage', $data, TRUE),
                )
            );
            exit;
        } else {
            echo json_encode(
                array(
                    'status' => false,
                )
            );
            exit;
        }
    }

    /**
     * @descripte   异步度假分润
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxLine()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        if(empty($this->uid)){
            $this->jsonMessages($msg = '用户未登陆', $url = 'ajaxMessages');
            exit;
        }
        $pg = $this->input->get('page') ? (int)$this->input->get('page') : '1';
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->line_order_profit->profit_page_list($page_num, $num, $getData);
        $data['more'] = ($data['profit']->num_rows() > 0) ? (($data['profit']->num_rows() >= 10) ? true:false ): false;
        /*统计收入*/
        $account = $this->line_order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['account'] = ($account->num_rows() > 0) ? $account->row()->account : '0';
        $not_account = $this->line_order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 1, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
        $data['not_account'] = ($not_account->num_rows() > 0) ? $not_account->row()->account : '0';
        $data['cat_name'] = array(
            1 => '自由行',
            2 => '自驾游',
            3 => '周边旅游',
            4 => '国内旅游',
            5 => '出境旅游',
            6 => '私人导游',
        );
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxLine', $data, TRUE),
                )
            );
            exit;
        } else {
            $this->jsonMessages($msg = '还没有线路分润信息', $url = 'ajaxMessages');
            exit;
        }
    }

    /**
     * @descripte   异步度假分润
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function ajaxLinePage()
    {
        $getData = $this->input->get();
        $status = array('ALL' => 0, 'NOT_ACCOUNT' => 1, 'ACCOUNT' => 2);
        if ( array_key_exists($getData['status'], $status) ) {
            $getData['status'] = $status[ $getData['status'] ];
        } else {
            $this->jsonMessages($msg = '分润状态错误', $url = 'ajaxMessages');
            exit;
        }
        /*时间限制*/
        $time_diff = strtotime($this->input->get('endTime')) - strtotime($this->input->get('startTime'));
        if ( $time_diff < 0 || $time_diff > 16416000 ) {
            $getData['startTime'] = date('Y-m-d', strtotime('-1 month'));
            $getData['endTime'] = date('Y-m-d');
        }
        $getData['uid'] = $this->uid;
        $pg = $this->input->get('pg');
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $data['profit'] = $this->line_order_profit->profit_page_list($page_num, $num, $getData);
        /*统计收入*/
//        $account = $this->line_order_profit->profit_account($getData = array('uid' => $getData['uid'], 'payer_uid' => $getData['payer_uid'], 'status' => 2, 'startTime' => $getData['startTime'], 'endTime' => $getData['endTime']));
//        $data['account'] = ($account->num_rows() > 0) ? $account->row()->account : '0';
        $data['cat_name'] = array(
            1 => '自由行',
            2 => '自驾游',
            3 => '周边旅游',
            4 => '国内旅游',
            5 => '出境旅游',
            6 => '私人导游',
        );
        if ( $data['profit']->num_rows() > 0 ) {
            echo json_encode(
                array(
                    'status' => TRUE,
                    'html'   => $this->load->view('home/memberprofit/ajaxLinePage', $data, TRUE),
                )
            );
            exit;
        } else {
            echo json_encode(
                array(
                    'status' => false,
                )
            );
            exit;
        }
    }

    /**
     * @descripte   酒店
     * @Author xiumao
     * @date 2016/6/29 0029 上午 11:44
     */
    public function ajaxHotel()
    {
        $data['message'] = '还没有酒店分润信息';
        echo json_encode(
            array(
                'status' => TRUE,
                'html'   => $this->load->view('home/memberprofit/ajaxMessages', $data, TRUE),
            )
        );
        exit;
    }

    /**
     * @descripte   下级会员
     * @Author xiumao
     * @date 2016/6/30 0030 下午 1:44
     */
    public function juniorMember()
    {
        $getData['parent_id'] = $this->uid;
        if(empty($this->uid)){
            $getData['parent_id'] = -1;
        }
        $search = $this->input->post('search');
        $getData['search'] = !empty($search) ? $this->input->post('search', true) : '';
        $this->data['search'] = $getData['search'];
        $res = $this->user->page_list($page_num = 10, $num = 0, $getData);
        if ( $res->num_rows() > 0 ) {
            foreach ( $res->result() as $v ) {
                $v->user_type_name = $this->userType($v->user_type);
            }
        }
        $this->data['juniorMember'] = $res;
        $this->data['more'] = ($res->num_rows() > 0) ? (($res->num_rows() >= 10) ? true:false ): false;

        $this->load->view('home/memberprofit/juniorMember', $this->data);
    }

    /**
     * @descripte   下级会员分页
     * @Author xiumao
     * @date 2016/6/30 0030 下午 1:44
     */
    public function ajaxJuniorMember()
    {
        $pg = $this->input->get('pg');
        $page_num = 10;
        $num = ($pg - 1) * $page_num;
        $getData['parent_id'] = $this->uid;
        $search = $this->input->post('search');
        $getData['search'] = !empty($search) ? $this->input->post('search', true) : '';
        $res = $this->user->page_list($page_num, $num, $getData);
        if ( $res->num_rows() > 0 ) {
            foreach ( $res->result() as $v ) {
                $v->user_type_name = $this->userType($v->user_type);
            }
        }
        $data['juniorMember'] = $res;

        if ( $res->num_rows() > 0 ) {
            $this->ajaxJson(['status' => TRUE, 'html' => $this->load->view('home/memberprofit/ajaxJuniorMember', $data, TRUE)]);
        } else {
            $this->ajaxJson(['status' => false]);
        }
    }

    /**
     * @descripte   分润详情
     * @Author xiumao
     * @date 2016/6/27 0027 上午 11:05
     */
    public function profitDetail($m_id)
    {
        $this->data['userInfoNum'] = $this->user->findOne($m_id);
        if ($this->data['userInfoNum']->num_rows() > 0) {
            $this->data['userInfo'] = $this->data['userInfoNum']->row();
        }

        $this->load->view('home/memberprofit/profitDetail', $this->data);
    }

    /**
     * @descripte   提示
     * @Author xiumao
     * @date 2016/7/5 0005 上午 10:58
     */
    public function jsonMessages($msg, $url)
    {
        $data['message'] = $msg;
        echo json_encode(
            array(
                'status' => false,
                'html'   => $this->load->view('home/memberprofit/' . $url, $data, TRUE),
            )
        );
    }
}